/**
 * Software License, Version 1.0
 * 
 * Copyright 2008 The Trustees of Indiana University.  All rights reserved.
 * 
 *
 *Redistribution and use in source and binary forms, with or without 
 *modification, are permitted provided that the following conditions are met:
 *
 *1) All redistributions of source code must retain the above copyright notice,
 * the list of authors in the original source code, this list of conditions and
 * the disclaimer listed in this license;
 *2) All redistributions in binary form must reproduce the above copyright 
 * notice, this list of conditions and the disclaimer listed in this license in
 * the documentation and/or other materials provided with the distribution;
 *3) Any documentation included with all redistributions must include the 
 * following acknowledgment:
 *
 *"This product includes software developed by the Community Grids Lab. For 
 * further information contact the Community Grids Lab at 
 * http://communitygrids.iu.edu/."
 *
 * Alternatively, this acknowledgment may appear in the software itself, and 
 * wherever such third-party acknowledgments normally appear.
 * 
 *4) The name Indiana University or Community Grids Lab or Granules, 
 * shall not be used to endorse or promote products derived from this software 
 * without prior written permission from Indiana University.  For written 
 * permission, please contact the Advanced Research and Technology Institute 
 * ("ARTI") at 351 West 10th Street, Indianapolis, Indiana 46202.
 *5) Products derived from this software may not be called Granules, 
 * nor may Indiana University or Community Grids Lab or Granules appear
 * in their name, without prior written permission of ARTI.
 * 
 *
 * Indiana University provides no reassurances that the source code provided 
 * does not infringe the patent or any other intellectual property rights of 
 * any other entity.  Indiana University disclaims any liability to any 
 * recipient for claims brought by any other entity based on infringement of 
 * intellectual property rights or otherwise.  
 *
 * LICENSEE UNDERSTANDS THAT SOFTWARE IS PROVIDED "AS IS" FOR WHICH NO 
 * WARRANTIES AS TO CAPABILITIES OR ACCURACY ARE MADE. INDIANA UNIVERSITY GIVES
 * NO WARRANTIES AND MAKES NO REPRESENTATION THAT SOFTWARE IS FREE OF 
 * INFRINGEMENT OF THIRD PARTY PATENT, COPYRIGHT, OR OTHER PROPRIETARY RIGHTS. 
 * INDIANA UNIVERSITY MAKES NO WARRANTIES THAT SOFTWARE IS FREE FROM "BUGS", 
 * "VIRUSES", "TROJAN HORSES", "TRAP DOORS", "WORMS", OR OTHER HARMFUL CODE.  
 * LICENSEE ASSUMES THE ENTIRE RISK AS TO THE PERFORMANCE OF SOFTWARE AND/OR 
 * ASSOCIATED MATERIALS, AND TO THE PERFORMANCE AND VALIDITY OF INFORMATION 
 * GENERATED USING SOFTWARE.
 */
package cgl.granules.samples.sort;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.Properties;
import java.util.Random;
import java.util.StringTokenizer;

import cgl.granules.exception.MarshallingException;
import cgl.granules.results.ResultPayload;
import cgl.narada.event.NBEvent;
import cgl.narada.event.NBEventException;
import cgl.narada.event.TemplateProfileAndSynopsisTypes;
import cgl.narada.service.ServiceException;
import cgl.narada.service.client.ClientService;
import cgl.narada.service.client.EventProducer;
import cgl.narada.service.client.SessionService;

/**
 * A class that will generate numbers (to be sorted) continually.
 * 
 * @author Shrideep Pallickara
 * 
 */
public class NumberGenerator {

    private int entityId;

    private ClientService clientService;

    private String moduleName = "NumberGenerator: ";

    private EventProducer producer;

    private ResultPayload resultPayload;
    private String topic = "NumberStreams/Resource";
    private int numOfBuckets = 10;
    private int sizeOfBucket = 5000;
    private LinkedList<long[]> buckets;
    private int[] generationInfo;
    private long multiple;
    private Random random;
    private long numbersSent = 0;
    private Hashtable<String, String> streamsPublishedOn;


    public NumberGenerator(int numberOfBuckets) throws ServiceException {
        this.numOfBuckets = numberOfBuckets;
        random = new Random();
        entityId = random.nextInt();
        clientService = SessionService.getClientService(entityId);
        resultPayload = ResultPayload.getInstance();
        streamsPublishedOn = new Hashtable<String, String>();
        initialize();
    }


    private void initialize() {
        buckets = new LinkedList<long[]>();

        multiple = Long.MAX_VALUE / numOfBuckets;
        generationInfo = new int[numOfBuckets];
        System.out.println("Multiple = " + multiple);
        for (int i = 0; i < numOfBuckets; i++) {
            long[] generatedNumbers = new long[sizeOfBucket];
            buckets.add(generatedNumbers);
        }
    }


    private void manageGeneratedNumber(long generatedNumber)
        throws ServiceException, NBEventException, IOException,
        MarshallingException {
        /* First identify the bucket to put this into */
        long index = generatedNumber / multiple;
        int bucketIndex = (int) index;

        /* Retrieve the bucket in question */
        int numOfBucketEntries = generationInfo[bucketIndex];
        long[] bucketInQuestion = buckets.get(bucketIndex);

        /* add generated value to the bucket in question, at proper location */
        bucketInQuestion[numOfBucketEntries] = generatedNumber;

        /* check to see if the bucket needs to be drained */
        numOfBucketEntries++;
        if (numOfBucketEntries == sizeOfBucket) {
            // System.out.println("Emptying bucket (" + bucketIndex + ") ");
            String sendOverTopic = topic + bucketIndex;
            publishEvent(sendOverTopic, bucketInQuestion);
            numOfBucketEntries = 0;
        }
        generationInfo[bucketIndex] = numOfBucketEntries;
    }


    public void generate(int numbersToGenerate) throws ServiceException,
        NBEventException, IOException, MarshallingException {
        System.out.println("generating numbers ... ");

        long fraction = numbersToGenerate / 100;
        int percent = 0;
        for (int i = 0; i < numbersToGenerate; i++) {
            // if (i % 100000 == 0) {

            if (i % fraction == 0) {
                percent++;
                String report = "Generated (" + percent + "%) numbers.";
                long utilization = getPercentUtilization();
                report +=
                    " Memory: " + getMemoryUtilization() + "MB  ("
                        + utilization + "%) ";
                System.out.println(report);
                while (utilization > 70) {
                    System.runFinalization();
                    System.gc();
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    utilization = getPercentUtilization();
                    report =
                        "GC: Memory: " + getMemoryUtilization() + "MB  ("
                            + utilization + "%) ";
                    System.out.println(report);
                }

            }

            long generatedNumber = random.nextLong();
            if (generatedNumber < 0) {
                generatedNumber = Math.abs(generatedNumber);
            }

            if (generatedNumber < 0) {
                System.out.println("Generate number = " + generatedNumber);
            }

            manageGeneratedNumber(generatedNumber);
        }
        emptyAngPendingBuckets();
        publishComputeCommand();
        listStreamsPublishedOn();
    }


    private long getPercentUtilization() {
        Runtime runtime = Runtime.getRuntime();
        long totalMemory = runtime.totalMemory();
        long freeMemory = runtime.freeMemory();

        long memUtilization = totalMemory - freeMemory;

        long utilization =
            (long) (((double) memUtilization / (double) totalMemory) * 100);
        return utilization;
    }


    private long getMemoryUtilization() {
        Runtime runtime = Runtime.getRuntime();
        long totalMemory = runtime.totalMemory();
        long freeMemory = runtime.freeMemory();

        long memUtilization = totalMemory - freeMemory;
        return (memUtilization / (1024 * 1024));
    }


    private void emptyAngPendingBuckets() throws ServiceException,
        NBEventException, IOException, MarshallingException {
        for (int i = 0; i < numOfBuckets; i++) {
            int bucketIndex = i;
            int bucketEntries = generationInfo[bucketIndex];
            if (bucketEntries == 0) {
                continue;
            }

            long[] partialBucket = new long[bucketEntries];
            long[] bucketInQuestion = buckets.get(bucketIndex);
            System.arraycopy(bucketInQuestion, 0, partialBucket, 0,
                             bucketEntries);
            // System.out.println("Emptying bucket (" + bucketIndex + ") ");
            String sendOverTopic = topic + bucketIndex;
            publishEvent(sendOverTopic, partialBucket);

        }
    }


    public void initializeBrokerCommunications(Properties props, String commType)
        throws ServiceException {
        clientService.initializeBrokerCommunications(props, commType);
    }


    /**
     * Closes the connection to the broker.
     * 
     * @throws ServiceException
     */
    public void closeBrokerConnection() throws ServiceException {

        clientService.closeBrokerConnection();
        clientService.terminateServices();
        try {
            /** Make sure that the transmission tracker is shutdown gracefully. */
            Thread.sleep(100);
        } catch (InterruptedException e) {
            System.out.println(moduleName
                + "Problems sleeping while waiting for transfer completion");
        }
    }


    public void initializeProducer() throws ServiceException {
        producer = clientService.createEventProducer();

        producer.generateEventIdentifier(true);
        producer.setTemplateId(12345);
        producer.setDisableTimestamp(false);

        producer.setSuppressRedistributionToSource(true);
    }


    public void publishComputeCommand() throws ServiceException,
        NBEventException, IOException {
        String topicInfo = "NumberStreams/Compute";

        NBEvent nbEvent =
            producer.generateEvent(TemplateProfileAndSynopsisTypes.STRING,
                                   topicInfo, topicInfo.getBytes());
        producer.publishEvent(nbEvent);

    }


    public synchronized void publishEvent(String publishingTopic,
                                          long[] generatedNumbers)
        throws ServiceException, NBEventException, IOException,
        MarshallingException {
        /*
         * if (numbersSent > 500000) { try { Thread.sleep(500); } catch
         * (InterruptedException e) { e.printStackTrace(); } numbersSent = 0; }
         */

        if (!streamsPublishedOn.containsKey(publishingTopic)) {
            streamsPublishedOn.put(publishingTopic, publishingTopic);
        }

        
        NBEvent nbEvent =
            producer.generateEvent(TemplateProfileAndSynopsisTypes.STRING,
                                   publishingTopic,
                                   resultPayload.getBytes(generatedNumbers));
        producer.publishEvent(nbEvent);
        numbersSent += generatedNumbers.length;
    }


    private void listStreamsPublishedOn() {
        int numOfStreamsPublished = streamsPublishedOn.size();
        StringBuffer strBuf = new StringBuffer();

        strBuf.append("Published over (" + numOfStreamsPublished
            + ") streams \n");
        Enumeration<String> streamInfo = streamsPublishedOn.keys();

        while (streamInfo.hasMoreElements()) {
            strBuf.append(streamInfo.nextElement());
            strBuf.append("\n");
        }
        strBuf.append("\n");
        System.out.println(strBuf);

    }


    public static void main(String[] args) {
        try {
            String module = "NumberGenerator.main() ->";
            if (args.length < 3) {
                System.out.println("Usage: java cgl.granules.samples.sort"
                    + "NumberGenerator <hostname> <port> <numOfBuckets>");
                System.exit(0);
            }

            String hostname = args[0];
            String portnum = args[1];
            int numOfBuckets = Integer.parseInt(args[2]);

            String transport = "niotcp";
            if (args.length > 3) {
                transport = args[3];
            }

            NumberGenerator numberGenerator = new NumberGenerator(numOfBuckets);
            Properties props = new Properties();

            /** These properties pertain to setting up a TCP link */
            props.put("hostname", hostname);
            props.put("portnum", portnum);

            numberGenerator.initializeBrokerCommunications(props, transport);
            numberGenerator.initializeProducer();

            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader commandLine = new BufferedReader(isr);
            String helpInfo =
                "Generates a whole bunch of longs for sorting:\n"
                    + "generate <how_many_million> : Generate a set of numbers\n"
                    + "h                   : List commands";
            StringTokenizer tok;
            while (true) {
                String s = commandLine.readLine();
                if (s == null) {
                    System.out.println(module + "String is null!!!");
                    break;
                }

                if (s.equals("h")) {
                    System.out.println(helpInfo);
                    continue;
                }

                if (s.startsWith("generate")) {
                    tok = new StringTokenizer(s, " ");
                    int numbersToGenerate = 1000000;

                    tok.nextToken();
                    if (tok.countTokens() > 1) {
                        numbersToGenerate = Integer.parseInt(tok.nextToken());
                        numbersToGenerate *= 1000000;
                    }

                    long start = System.currentTimeMillis();
                    numberGenerator.generate(numbersToGenerate);
                    long overhead = System.currentTimeMillis() - start;

                    System.out.println("Generation overhead=[" + overhead
                        / 1000 + "] seconds.");
                    continue;
                }
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}